<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/admin/adminPage/AdminIndex' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加首页轮播图</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <div style="margin-left: 200px">
      <el-upload
          :action="'http://localhost:9580/upload?dir='+this.dir"
          name="picFile"
          :limit="1"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
      <el-button type="primary" @click="addBanner()">添加</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dir: '首页轮播图',
      dialogImageUrl: '',
      dialogVisible: false,
      hotelIndexBanner: {
        url: ''
      }
    }
  },
  methods: {
    addBanner() {
      let url = 'http://localhost:9580/hotelIndexBanners/admin/addHotelIndexBanner';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.hotelIndexBanner);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            showClose: true,
            message: '添加轮播图成功！',
            type: 'success'
          });
          this.$router.push('/admin/adminPage/SystemIndex');
        } else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'error'
          });
        }
      });
    },
    handleSuccess(response, file, fileList) {
      this.hotelIndexBanner.url = response.data;
      console.log(this.hotelIndexBanner.url);
    },
    handleRemove(file, fileList) {

      let url = 'http://localhost:9580/remove?url=' + this.hotelIndexBanner.url;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
          .get(url).then(function () {

      })
      this.hotelIndexBanner.url = '';
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }
}
</script>

<style scoped>

</style>